import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Rotator {
    final static int CHAR_NUMBER = 27;//26个字母和一个空格
    private List<Integer> link;
    private List<Integer> rlink;
    private int count,point,type;
    public Rotator(List<Integer> link, int point, int type) {
        super();
        this.link = link;
        Integer[] array = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};
        rlink = new ArrayList<Integer>();
        Collections.addAll(rlink, array);
        for(int i = 0; i < link.size(); i++) {
            rlink.set(link.get(i), i);
        }
        this.point = point;
        this.type = type;
        count = 0;
    }
    int getNum(int offset) {//所选数字为目前最低位的指针加上输入的偏移量
        count++;
        int sequence = offset + point;
        if(sequence >= CHAR_NUMBER) sequence = sequence%CHAR_NUMBER;
        //先加charnumber使之变为正数，再取其余数
        int num = (link.get(sequence) - point + CHAR_NUMBER)%CHAR_NUMBER;
        return num;
    }
    int getNumBack(int offset) {
        count++;
        int sequence = offset + point;
        if(sequence >= CHAR_NUMBER) sequence = sequence%CHAR_NUMBER;
        int num = (rlink.get(sequence) - point + CHAR_NUMBER)%CHAR_NUMBER;
        switch(type) {
            case 1: {//最低级转轮每次读取都转一次，字符进入后再出来共读取两次
                if(count%2 == 0) {
                    point++;
                    count = 0;
                }
                break;
            }
            case 2: {//最低轮转一圈第二轮转一刻
                if(count%CHAR_NUMBER == 0) {
                    point++;
                    count = 0;
                }
                break;
            }
            case 3: {
                if(count%(CHAR_NUMBER*CHAR_NUMBER) == 0) {
                    point++;
                    count = 0;
                }
                break;
            }
            default:{
                break;
            }
        }
        return num;
    }
    void setPoint(int point) {
        this.point = point;
        count = 0;
    }
    int getPoint() {
        return point;
    }
}
